<template>
  <div>
    <el-dialog
      title="文件信息"
      :visible.sync="show"
      width="40%"
      :before-close="handleClose">
      <div>
        <el-form ref="form" :model="editInfoForm" label-width="80px">
          <el-form-item label="名称">
            <el-input v-model="editInfoForm.mediaName"></el-input>
          </el-form-item>
          <el-form-item label="网址">
            <el-input v-model="editInfoForm.location"></el-input>
          </el-form-item>
          <el-form-item label="缩略图">
            <template v-if="!editInfoForm.image">
              <el-upload
                size="mini"
                style="width: 100%;"
                class="upload-demo"
                drag
                :on-success="uploadImageSuccess"
                :action="uploadUrl"
                multiple="false"
                :accept="imageFormat"
                @drop.native="e => beforeUploadImage(e.dataTransfer.files[0])"
              >
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              </el-upload>
            </template>
            <div v-else>
              <div @click="delImageIcon" class="el-icon-circle-close close-file-icon deleteFile"></div>
              <el-image
                style="width: 100px; height: 100px"
                :src="editInfoForm.image"
                fit="cover">
              </el-image>
            </div>
          </el-form-item>
          <el-form-item label="分组">
            <el-select
              v-model="editInfoForm.groupId"
              placeholder="组名"
              clearable
            >
              <el-option
                v-for="(item, index) in groupList"
                :label="item.group_name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="confirmUpdateFileInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as distributeService from "@/views/distribute/service";
import * as service from './service'

export default {
  name: "index",
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    mediaType: {
      type: [Number, String],
      default: ''
    },
    editInfoForm: {
      type: Object,
      default: () => {
        return {
          mediaName: '',
          location: '',
          image: '',
          groupId: '',
          mediaSuffix: ''
        }
      }
    }
  },
  data() {
    return {
      imageFormat: ".jpg,.png,.jpeg,.JPG,.PNG,.JPEG",
      groupList: [],
      uploadUrl: window.ajaxUploadUrl + '/common/fileApi/file',

    }
  },
  watch: {
    show(val) {
      if(val) {
        this.getGroupData()
        console.log('editInfoForm ---', this.mediaType)
      }
    }
  },
  mounted() {

  },
  methods: {
    beforeUploadImage(file) {
      var suffix = "."+file.name.substring(file.name.lastIndexOf(".") + 1);
      if(!this.imageFormat.includes(suffix)){
        this.$message.error('文件格式错误')
        return false;
      }
      return true;
    },
    getGroupData() {
      const params = {
        page: 1,
        pageSize: 200000000,
        mediaType: this.mediaType
      }

      distributeService.getFileGroupList(params).then((res) => {
        this.groupList = res.data.records
      })
    },
    uploadImageSuccess(res, file) {
      this.editInfoForm.image = res.data[0].file
    },
    delFileIcon() {
      this.editInfoForm.image = ''
    },
    handleClose() {
      this.$emit('on-close')
    },
    confirmUpdateFileInfo() {
      const params = Object.assign({}, this.editInfoForm)
      // if(params.mediaSuffix) {
      //   params.mediaName = `${params.mediaName}.${params.mediaSuffix}`
      // }

      delete params.mediaSuffix
      service.saveFileContent(params).then(() => {
        this.$message.success('修改成功')
        this.handleClose()
      })
    }
  }
}
</script>

<style scoped>
  ::v-deep .el-form-item__label{
    text-align: right;
  }
  .deleteFile{
    position: absolute;
    z-index: 1;
    left: 90px;
    top: -5px;
    cursor: pointer;
    color: red;
  }

</style>
